# -*- coding: utf-8 -*-
from datetime import timedelta, datetime
from time import time

from utils.operators.rich_sql_sensor import RichSqlSensor

label = f"dwd_departure_transfer_timely_route_{int(time())}"
timeout = timedelta(minutes=20).seconds
doris_jms_dwd__dwd_departure_transfer_timely_route = RichSqlSensor(
    task_id='doris_jms_dwd__dwd_departure_transfer_timely_route',
    email=['zhangqinglin@jtexpress.com', 'yl_bigdata@yl-scm.com'],
    pool='broker_load_pool',
    conn_id='new_doris',
    retries=0,
    pre_sql=f"""
             TRUNCATE TABLE jms_dwd.dwd_departure_transfer_timely_route PARTITION (p{{{{ execution_date | date_add(-1) | cst_ds_nodash }}}},p{{{{ execution_date | date_add(-2) | cst_ds_nodash }}}});
             LOAD LABEL jms_dwd.{label} (
                 DATA INFILE("hdfs://yl-bg-hdp/dw/hive/jms_dm.db/external/dm_cn_departure_transfer_timely_route_dt/dt={{{{ execution_date | date_add(-1) | cst_ds }}}}/*") 
                 INTO TABLE dwd_departure_transfer_timely_route FORMAT AS 'PARQUET'
                 (billcode,start_code,start_name,order_source_code,order_source_name,start_agent_code,start_agent_name,center_type,nextstationcode,nextstation,send_scantime,send_shipment_no,planned_departure_time,actual_departure_time,next_planned_arrival_time,packagecode,goods_type_code,goods_type_name,arrival_shipment_no,arrival_time,all_line_arrival_time,last_network_code,last_network_name,transfer_hours,route_send_time,all_line_route_send_time,is_timely,untimely_cause,all_line_is_timely,all_line_untimely_cause,mage_region_name,mage_region_code,sign_agent_code,sign_agent_name,end_center_code,end_center_name,center_arrvial_time,terminal_dispatch_code,is_store,express_type_code,express_type_name,start_provider_id,start_provider_desc,last_send_shipment_no,last_arrival_shipment_no,reserved_seconds,arrival_actual_arrival_time,arrival_planned_arrival_time)
                 SET (dt = DATE('{{{{ execution_date | date_add(-1) | cst_ds }}}}')),
                 DATA INFILE("hdfs://yl-bg-hdp/dw/hive/jms_dm.db/external/dm_cn_departure_transfer_timely_route_dt/dt={{{{ execution_date | date_add(-2) | cst_ds }}}}/*") 
                 INTO TABLE dwd_departure_transfer_timely_route FORMAT AS 'PARQUET'
                 (billcode,start_code,start_name,order_source_code,order_source_name,start_agent_code,start_agent_name,center_type,nextstationcode,nextstation,send_scantime,send_shipment_no,planned_departure_time,actual_departure_time,next_planned_arrival_time,packagecode,goods_type_code,goods_type_name,arrival_shipment_no,arrival_time,all_line_arrival_time,last_network_code,last_network_name,transfer_hours,route_send_time,all_line_route_send_time,is_timely,untimely_cause,all_line_is_timely,all_line_untimely_cause,mage_region_name,mage_region_code,sign_agent_code,sign_agent_name,end_center_code,end_center_name,center_arrvial_time,terminal_dispatch_code,is_store,express_type_code,express_type_name,start_provider_id,start_provider_desc,last_send_shipment_no,last_arrival_shipment_no,reserved_seconds,arrival_actual_arrival_time,arrival_planned_arrival_time)
                 SET (dt = DATE('{{{{ execution_date | date_add(-2) | cst_ds }}}}'))
              )
             WITH BROKER '{{{{ var.json.doris_brokers | random_choice }}}}'
             PROPERTIES ('timeout'='{timeout}', 'max_filter_ratio'='0.0')""",
    poke_sql=f"SHOW LOAD FROM jms_dwd WHERE label = '{label}' ORDER BY CreateTime DESC LIMIT 1",
    sql_on_kill=f"CANCEL LOAD FROM jms_dwd WHERE LABEL = '{label}'",
    success=lambda r: r[2] == 'FINISHED',
    failure=lambda r: (r[2] is not None and r[2] == 'CANCELLED', str(r[7])),
    poke_interval=60,
    # execution_timeout=max(datetime.now().replace(hour=8, minute=25, second=0) - datetime.now(), timedelta(seconds=1)),
)

